package com.nineclock.system.controller;

import com.nineclock.common.entity.Result;
import com.nineclock.system.SysCompanyControllerApi;
import com.nineclock.system.dto.*;
import com.nineclock.system.service.SysCompanyService;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/company")
public class SysCompanyController implements SysCompanyControllerApi {

    @Autowired
    SysCompanyService companyService;

    @Autowired
    SysCompanyUserService companyUserService;

    @Override
    @GetMapping
    public Result<SysCompanyDTO> queryCompanyInfo() {
        SysCompanyDTO companyDTO = companyService.queryCompanyInfo();
        return Result.success(companyDTO);
    }

    @Override
    @PostMapping("/uploadOSS")
    public Result<String> uploadOSS(MultipartFile file) throws IOException {
        String imgPath = companyService.uploadOSS(file);
        return Result.success(imgPath);
    }

    @Override
    @PutMapping
    public Result updateCompanyInfo(@RequestBody SysCompanyDTO companyDTO) {
        companyService.updateCompanyInfo(companyDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> getCurrentAdmin() {
        SysCompanyUserDTO companyUserDTO = companyUserService.getCurrentAdmin();
        return Result.success(companyUserDTO);
    }

    @Override
    @PostMapping("/config/admin")
    public Result changeAdmin(@RequestBody SysChangeMainManagerDTO changeMainManagerDTO) {
        companyService.changeAdmin(changeMainManagerDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/list")
    public Result<List<SysCompanyDTO>> queryCompanyByName(String keyword) {
        List<SysCompanyDTO> companyDTOList = companyService.queryCompanyByName(keyword);
        return Result.success(companyDTOList);
    }

    @Override
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO sysApplyJoinCompanyUserDTO) {
        companyService.applyJoinCompany(sysApplyJoinCompanyUserDTO);
        return Result.success();
    }

    @Override
    @PostMapping("/allowedJoinCompany")
    public Result allowedJoinCompany( SysAllowedJoinCompanyUserDTO sysAllowedJoinCompanyUserDTO) {
        companyService.allowJoinCompany(sysAllowedJoinCompanyUserDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/list/{userId}")
    public Result<List<SysCompanyDTO>> queryCompanyListByUserId(@PathVariable Long userId) {
        List<SysCompanyDTO> companyDTOList = companyService.queryCompanyListByUserId(userId);
        return Result.success(companyDTOList);
    }
}
